<template>
	<uni-popup ref="popup" type="center" style="z-index: 910" :is-mask-click="false">
		<view class="package column account">
			<image :src="accpount" mode=""></image>
			<view>到账成功</view>
			<!-- space-between -->
			<view class="column list" v-for="item in collectList" :key='item.id'>
				<text>您有{{item.money}}元待领取的分拨资金</text>
				<text @click="receive(item.id)">立即领取</text>
			</view>
			<!-- <view @click="account()" class="but">查看账户</view> -->
		</view>
	</uni-popup>
</template>

<script setup lang="ts">
	import { accpount } from '@/utils/Static';
	import { ref } from 'vue';
	import { navTo } from '@/utils/navigator';
	import { onShow } from '@dcloudio/uni-app';
	import { toPublish } from '@mqtt';
	import { showLoading, errorToast, successToast } from '@/utils/prompt';
	import { getFenboOrder } from '@/gql/fenbo';
	import { useUserStore } from '@/stores/user';
	import { getDriverID } from '@/stores/driverID';
	const userInfo = useUserStore();
	const driver_id = getDriverID();
	/**
	 * 分拨账户领取/到账
	 */
	const popup = ref(null)
	const received = ref()
	const collectList = ref([])
	const emits = defineEmits(['changes']);
	onShow(() => {
		if (!userInfo.nickname) {
			userInfo.getUserInfo();
			setTimeout(()=>{
				revoke()
			},1000)
		}
	})
	revoke()
	/**
	 * 查询是否存在分拨金额
	 */
	function revoke() {
		if (!userInfo.mobile) {
			return
		}
		showLoading()
		const payload = {
			mobile: userInfo.mobile,
			driver_id
		};
		toPublish('customers/getLogNotice', payload, (obj : any) => {
			uni.hideLoading();
			const { code, data } = obj;
			if (code === 1) {
				received.value = data
				represent(2)
			}
		});
	}
	function represent(num : number) {
		const payload = {
			query: getFenboOrder,
			variables: {
				driver_id
			},
		};
		toPublish('ql/customers/getFenboOrder', payload, (obj : any) => {
			const { data } = obj;
			collectList.value = data.getFenboOrder
			if (collectList.value.length > 0 && num != 1) {
				popup.value.open()
			} else if (collectList.value.length < 1) {
				popup.value.close()
			}
		});
	}
	// 领取
	function receive(num : number) {
		showLoading()
		const payload = {
			id: num
		};
		toPublish('customers/receiveNotice', payload, (obj : any) => {
			uni.hideLoading();
			const { code, msg } = obj;
			if (code === 1) {
				emits('changes');
				successToast(msg)
				represent(1)
			} else {
				errorToast(msg)
			}
		});
	}
	function account() {
		popup.value.close()
		navTo('team/alteration')
	}
	defineExpose({ revoke });
</script>

<style scoped lang="less">
	.account {
		width: 700rpx;

		image {
			width: 336rpx;
			height: 299rpx;
			margin-top: -180rpx;
		}

		view:nth-of-type(1) {
			font-size: 50rpx;
			font-weight: 500;
			color: #333333;
		}

		.list {
			// width: 600rpx;
			// height: 80rpx;
			// background: #EBF6FF;
			border-radius: 5px;
			padding: 0px 15rpx;
			box-sizing: border-box;
			margin-top: 30rpx;

			text:nth-of-type(1) {
				// width: 370rpx;
				font-size: 32rpx;
				color: #FB9A08;
			}

			// text:nth-of-type(2) {
			// 	width: 130rpx;
			// 	height: 50rpx;
			// 	background: linear-gradient(90deg, #0C75FF 0%, #179DFE 100%);
			// 	border-radius: 5px;
			// 	text-align: center;
			// 	line-height: 50rpx;
			// 	font-size: 26rpx;
			// 	color: #FFFFFF;
			// }
			text:nth-of-type(2) {
				width: 480rpx;
				height: 100rpx;
				background: linear-gradient(-10deg, #2C96F9, #1B91FF);
				border-radius: 5px;
				text-align: center;
				line-height: 100rpx;
				color: #fff;
				margin: 30rpx auto;
				font-size: 32rpx;
			}
		}

		.but {
			margin: 50rpx 0px;
			width: 450rpx;
			height: 90rpx;
			background: linear-gradient(90deg, #0C75FF 0%, #179DFE 100%);
			border-radius: 45rpx;
			font-size: 32rpx;
			font-weight: 500;
			color: #FFFFFF;
			text-align: center;
			line-height: 90rpx;
		}
	}
</style>